//
//  Storyboard.swift
//  HeadlineNews
//
//  Created by han on 2019/5/30.
//  Copyright © 2019 han. All rights reserved.
//

import UIKit

// 用于多个Storyboard

extension UIStoryboard {
    
    enum Storyboard: String {
        case Main
        case Home
        case Feature
        case Mine
    }
    
    // 便利构造器
    convenience init(storyboard: Storyboard, bundle: Bundle? = nil) {
        self.init(name: storyboard.rawValue, bundle: bundle)
    }
    
    // 类方法
    static func storyboard(storyboard: Storyboard, bundle: Bundle? = nil) -> UIStoryboard {
        return UIStoryboard.init(name: storyboard.rawValue, bundle: bundle)
    }
    
    func instantiateViewController<T: UIViewController>() -> T {
        guard let viewController = self.instantiateViewController(withIdentifier: T.storyboardIdentifier) as? T else {
            fatalError("\(T.storyboardIdentifier) is nonable")
        }
        return viewController
    }

}

protocol StoryboardIdentifiable {
    static var storyboardIdentifier: String { get }
}

extension StoryboardIdentifiable where Self: UIViewController {
    static var storyboardIdentifier: String {
        return String(describing: self)
    }
}

extension UIViewController: StoryboardIdentifiable {}
